package com.zzxz.practicaltrainingsys.util;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

public class AvatarUtil {

    public static String getAvatarUploadPath(String role) {
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            // 处理异常
        }
        if (path == null || !path.exists()) {
            path = new File("");
        }

        // 根据角色创建不同的头像目录
        File avatarPath = new File(path.getAbsolutePath(),
                "static/avatar/" + role.toLowerCase() + "/");
        if (!avatarPath.exists()) {
            avatarPath.mkdirs();
        }
        return avatarPath.getAbsolutePath();
    }

    public static String getAvatarAccessPath(String role, String fileName) {
        return "/avatar/" + role.toLowerCase() + "/" + fileName;
    }
}